package com.canyou.activiti;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.activiti.entity.ProcessInstanceAddDTO;
import com.canyou.activiti.entity.ProcessInstanceSuspendDTO;
import com.canyou.activiti.entity.ProcessInstanceVO;
import com.canyou.activiti.service.ProcessInstanceService;
import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.canyou.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import static com.canyou.api.ResponseBO.ok;

/**
 * 流程实例
 *
 * @author fy
 * @date 2021/3/3 14:49
 */
@RestController
@ApiSort(2002)
@Api(tags = "流程实例")
@RequestMapping("/process-instance")
@Validated
public class ProcessInstanceController extends BaseApiController {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @ApiOperation(value = "分页获取流程实例列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", paramType = "query", value = "当前第几页",
                    dataType = "int", required = false, example = PAGE_INDEX),
            @ApiImplicitParam(name = "pageSize", paramType = "query", value = "每页记录数",
                    dataType = "int", required = false, example = PAGE_SIZE),
            @ApiImplicitParam(name = "instanceName", paramType = "query", value = "流程实例名称",
                    dataType = "string", required = false, example = ""),
            @ApiImplicitParam(name = "processKey", paramType = "query", value = "流程定义key", dataType = "string")
    })
    @GetMapping("/get_page")
    public ResponseBO<IPage<ProcessInstanceVO>> getPageList(
            @RequestParam(defaultValue = PAGE_INDEX, required = false) int pageNumber,
            @RequestParam(defaultValue = PAGE_SIZE, required = false) int pageSize,
            @RequestParam(required = false) String instanceName,
            @RequestParam(required = false) String processKey) {
        IPage<ProcessInstanceVO> page = new Page<>(pageNumber, pageSize);
        IPage<ProcessInstanceVO> pageList = processInstanceService.getPageList(page, instanceName, processKey);
        return ok(pageList);
    }

    @ApiOperation(value = "删除流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", paramType = "query", value = "流程实例id",
                    dataType = "string", required = true, example = "68cacd2d-7806-11eb-b929-00ff042432dd"),
            @ApiImplicitParam(name = "deleteReason", paramType = "query", value = "删除原因",
                    dataType = "string", required = true, example = "想删就删")
    })
    @GetMapping("/remove")
    public ResponseBO remove(@NotBlank(message = "流程实例id不能为空") @RequestParam String processInstanceId,
                             @NotBlank(message = "删除原因不能为空") @RequestParam String deleteReason) {
        processInstanceService.deleteProcessInstance(processInstanceId, deleteReason);
        return ok("删除成功");
    }

    @ApiOperation(value = "激活/挂起流程实例")
    @PostMapping("/active_suspend")
    public ResponseBO activeOrSuspend(
            @ApiParam(name = "processInstanceSuspendDTO", type = "ProcessInstanceSuspendDTO", value = "激活/挂起流程实例数据", required = true)
            @Valid
            @NotNull(message = "激活/挂起流程实例信息不能为空")
            @RequestBody ProcessInstanceSuspendDTO processInstanceSuspendDTO) {
        processInstanceService.activeOrSuspendProcessInstance(processInstanceSuspendDTO.getProcessInstanceId(),
                processInstanceSuspendDTO.getSuspensionState());
        return ok(StringUtils.format("{}成功",
                Integer.valueOf(1).equals(processInstanceSuspendDTO.getSuspensionState()) ? "激活" : "挂起"));
    }

    @ApiOperation(value = "启动流程实例")
    @PostMapping("/start_process_instance")
    public ResponseBO<ProcessInstanceVO> startProcess(
            @ApiParam(name = "processInstanceAddDTO", type = "ProcessInstanceAddDTO", value = "启动流程实例数据", required = true)
            @Valid
            @NotNull(message = "启动流程实例信息不能为空")
            @RequestBody ProcessInstanceAddDTO processInstanceAddDTO) {
        return ok(processInstanceService.startProcessInstance(processInstanceAddDTO), "启动流程成功");
    }

}
